package ch.subgate.spritestudio.tools;

import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TabHost;
import ch.subgate.spritestudio.R;
import ch.subgate.spritestudio.widgets.APickerView;
import ch.subgate.spritestudio.widgets.HEXPresenterView;
import ch.subgate.spritestudio.widgets.HPickerView;
import ch.subgate.spritestudio.widgets.HSVAPresenterView;
import ch.subgate.spritestudio.widgets.RGBAPresenterView;
import ch.subgate.spritestudio.widgets.RGBASeekBar;
import ch.subgate.spritestudio.widgets.SVPickerView;

public class DialogHelper {
	public static void showColorPickerDialog(
			Activity activity,
			OnColorChangedListener listener,
			int oldColor) {
		// Open color picker dialog
		final Dialog dialog = new Dialog(activity);
		dialog.setContentView(R.layout.pickcolor_layout);
		dialog.setTitle(R.string.pickcolordialog_titletext);
		
		// Setup color picker tabs
		TabHost host = (TabHost)dialog.findViewById(android.R.id.tabhost);
		host.setup();
		
		TabHost.TabSpec tab = host.newTabSpec("hextab");
		tab.setIndicator("HEX");
		activity.getLayoutInflater().inflate(R.layout.pickcolor_hextab, host.getTabContentView(), true);
		tab.setContent(R.id.hextabroot);
		host.addTab(tab);
		
		tab = host.newTabSpec("hsvtab");
		tab.setIndicator("HSV");
		activity.getLayoutInflater().inflate(R.layout.pickcolor_hsvtab, host.getTabContentView(), true);
		tab.setContent(R.id.hsvtabroot);
		host.addTab(tab);
		
		tab = host.newTabSpec("rgbtab");
		tab.setIndicator("RGB");
		activity.getLayoutInflater().inflate(R.layout.pickcolor_rgbtab, host.getTabContentView(), true);
		tab.setContent(R.id.rgbtabroot);
		host.addTab(tab);
		
		// Setup Color presenters
		RGBAPresenterView rgbpresenter = (RGBAPresenterView)dialog.findViewById(R.id.rgbapresenter);
		rgbpresenter.setOldColor(oldColor);
		HSVAPresenterView hsvpresenter = (HSVAPresenterView)dialog.findViewById(R.id.hsvpresenter);
		hsvpresenter.setOldColor(oldColor);
		HEXPresenterView hexpresenter = (HEXPresenterView)dialog.findViewById(R.id.hexpresenter);
		hexpresenter.setOldColor(oldColor);
		
		// Setup RGBA tab content
		rgbpresenter.setOnColorChangedListener(listener);
		
		RGBASeekBar aseeker = (RGBASeekBar)dialog.findViewById(R.id.aseeker);
		aseeker.addRGBAListener(rgbpresenter);
		RGBASeekBar rseeker = (RGBASeekBar)dialog.findViewById(R.id.rseeker);
		rseeker.addRGBAListener(rgbpresenter);
		RGBASeekBar gseeker = (RGBASeekBar)dialog.findViewById(R.id.gseeker);
		gseeker.addRGBAListener(rgbpresenter);
		RGBASeekBar bseeker = (RGBASeekBar)dialog.findViewById(R.id.bseeker);
		bseeker.addRGBAListener(rgbpresenter);
		
		aseeker.addRGBAListener(rseeker);
		aseeker.addRGBAListener(gseeker);
		aseeker.addRGBAListener(bseeker);
		rseeker.addRGBAListener(aseeker);
		rseeker.addRGBAListener(gseeker);
		rseeker.addRGBAListener(bseeker);
		gseeker.addRGBAListener(aseeker);
		gseeker.addRGBAListener(rseeker);
		gseeker.addRGBAListener(bseeker);
		bseeker.addRGBAListener(aseeker);
		bseeker.addRGBAListener(rseeker);
		bseeker.addRGBAListener(gseeker);
		
		aseeker.init();
		rseeker.init();
		gseeker.init();
		bseeker.init();
		
		// Setup HSVA tab content
		HPickerView hpicker = (HPickerView)dialog.findViewById(R.id.hpickerview);
		SVPickerView svpicker = (SVPickerView)dialog.findViewById(R.id.svpickerview);
		APickerView apicker = (APickerView)dialog.findViewById(R.id.apickerview);
		
		hpicker.registerHueChangedListener(svpicker);
		hpicker.registerHueChangedListener(apicker);
		hpicker.registerHueChangedListener(hsvpresenter);
		svpicker.registerSaturationListener(apicker);
		svpicker.registerSaturationListener(hsvpresenter);
		svpicker.registerValueListener(apicker);
		svpicker.registerValueListener(hsvpresenter);
		apicker.registerAlphaListener(hsvpresenter);
		hsvpresenter.setOnColorChangedListener(listener);
		
		// Setup HEX tab content
		EditText hexedit = (EditText)dialog.findViewById(R.id.hexedit);
		
		hexedit.addTextChangedListener(hexpresenter);
		hexpresenter.setOnColorChangedListener(listener);
		
		Button button = (Button)dialog.findViewById(R.id.hexok);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				dialog.dismiss();
			}
		});
		button = (Button)dialog.findViewById(R.id.hsvaok);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				dialog.dismiss();
			}
		});
		button = (Button)dialog.findViewById(R.id.rgbaok);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View view) {
				dialog.dismiss();
			}
		});
		
		dialog.show();
	}
}
